/*------------------------------------------------------
                hou_elearningGEN beta v1.0
Author: xtrung.net@gmail.com
Write On: 04/27/2008
Create On:4/13/2012 11:22:31 AM
------------------------------------------------------*/
using hou_elearning.Entity;
using hou_elearning.DataAccess;
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Text;
namespace hou_elearning.Business
{
    public class ThongtinbaikiemtraBRL
    {
        #region Init
        private static string EX_NOT_EXIST="Không Tồn Tại Thongtinbaikiemtra Này";
		private static string EX_FK_IHOCVIENID_INVALID="FK_iHocvienID không hợp lệ";
		private static string EX_FK_IKIEMTRAID_INVALID="FK_iKiemtraID không hợp lệ";
		private static string EX_IDIEM_INVALID="iDiem không hợp lệ";
		private static string EX_ITRANGTHAI_INVALID="iTrangthai không hợp lệ";
		private static string EX_DTHOIGIANNOPBAI_INVALID="dThoigiannopbai không hợp lệ";
		private static string EX_FK_IGIANGVIENCHAMBAIID_INVALID="FK_iGiangvienchambaiID không hợp lệ";
		private static string EX_ID_INVALID="PK_iThongtinbaikiemtraID không hợp lệ";
        #endregion
        #region Public Methods
        /// <summary>
        /// Get Thongtinbaikiemtra Theo ID
        /// </summary>
        /// <param name="PK_iThongtinbaikiemtraID">Int64:Thongtinbaikiemtra ID</param>
        /// <returns>ThongtinbaikiemtraEntity</returns>        
        public static ThongtinbaikiemtraEntity GetOne(Int64 PK_iThongtinbaikiemtraID)
        {
            
			if(PK_iThongtinbaikiemtraID<=0)
				throw new Exception(EX_ID_INVALID);
            return ThongtinbaikiemtraDAL.GetOne(PK_iThongtinbaikiemtraID);
        }
        /// <summary>
        /// Lấy về List các Thongtinbaikiemtra
        /// </summary>
        /// <returns>List ThongtinbaikiemtraEntity:List Thongtinbaikiemtra Cần lấy</returns>
        public static List<ThongtinbaikiemtraEntity> GetAll()
        {
            return ThongtinbaikiemtraDAL.GetAll();
        }
        public static List<ThongtinbaikiemtraEntity> GetByFK_iHocvienID(Int64 FK_iHocvienID)
		{
			if(FK_iHocvienID<=0)
				throw new Exception(EX_FK_IHOCVIENID_INVALID);
			return ThongtinbaikiemtraDAL.GetByFK_iHocvienID(FK_iHocvienID);
		}public static List<ThongtinbaikiemtraEntity> GetByFK_iKiemtraID(Int64 FK_iKiemtraID)
		{
			if(FK_iKiemtraID<=0)
				throw new Exception(EX_FK_IKIEMTRAID_INVALID);
			return ThongtinbaikiemtraDAL.GetByFK_iKiemtraID(FK_iKiemtraID);
		}public static List<ThongtinbaikiemtraEntity> GetByFK_iGiangvienchambaiID(Int64 FK_iGiangvienchambaiID)
		{
			if(FK_iGiangvienchambaiID<=0)
				throw new Exception(EX_FK_IGIANGVIENCHAMBAIID_INVALID);
			return ThongtinbaikiemtraDAL.GetByFK_iGiangvienchambaiID(FK_iGiangvienchambaiID);
		}
        public static List<ThongtinbaikiemtraEntity> GetBy_FK_iKiemtraID_iTrangthai(Int64 FK_iKiemtraID, Int16 iTrangthai)
        {
            if (iTrangthai < 0)
                throw new Exception(EX_ITRANGTHAI_INVALID);
            if (FK_iKiemtraID <= 0)
                throw new Exception(EX_FK_IKIEMTRAID_INVALID);
            return ThongtinbaikiemtraDAL.GetBy_FK_iKiemtraID_iTrangthai(FK_iKiemtraID,iTrangthai);
        }
        /// <summary>
        /// Kiểm tra và thêm mới Thongtinbaikiemtra
        /// </summary>
        /// <param name="entity">Entity</param>
        /// <returns>Int32: ID của Thongtinbaikiemtra Mới Thêm Vào</returns>
        public static Int32 Add(ThongtinbaikiemtraEntity entity)
        {
            checkLogic(entity);
            checkDuplicate(entity, false);
            checkFK(entity);
            return ThongtinbaikiemtraDAL.Add(entity);
        }
        public static Int32 GetByFK_iHocvienID_FK_iKiemtraID(ThongtinbaikiemtraEntity entity)
        {
            checkLogic(entity);
            return ThongtinbaikiemtraDAL.GetByFK_iHocvienID_FK_iKiemtraID(entity);
        }
        public static Int32 Add_non_FK_iGiangvienchambaiID(ThongtinbaikiemtraEntity entity)
        {
            checkLogic(entity);
            checkDuplicate(entity, false);
            //   checkFK(entity);
            return ThongtinbaikiemtraDAL.Add_non_FK_iGiangvienchambaiID(entity);
        }
        /// <summary>
        /// Kiểm tra và chỉnh sửa Thongtinbaikiemtra
        /// </summary>
        /// <param name="entity">ThongtinbaikiemtraEntity</param>
        /// <returns>bool:kết quả thực hiện</returns>
        public static bool Edit(ThongtinbaikiemtraEntity entity)
        {
            checkExist(entity.PK_iThongtinbaikiemtraID);
            checkLogic(entity);
            checkDuplicate(entity, true);
            checkFK(entity);
            return ThongtinbaikiemtraDAL.Edit(entity);
        }
        public static bool Update_non_FK_iGiangvienchambaiID(ThongtinbaikiemtraEntity entity)
        {
            checkExist(entity.PK_iThongtinbaikiemtraID);
            checkLogic(entity);
            return ThongtinbaikiemtraDAL.Update_non_FK_iGiangvienchambaiID(entity);
        }
        /// <summary>
        /// Kiểm tra và xoá Thongtinbaikiemtra
        /// </summary>
        /// <param name="PK_iThongtinbaikiemtraID">Int64 : PK_iThongtinbaikiemtraID</param>
        /// <returns>bool:kết quả thực hiện</returns>
        public static bool Remove(Int64 PK_iThongtinbaikiemtraID)
        {
            checkExist(PK_iThongtinbaikiemtraID);
            return ThongtinbaikiemtraDAL.Remove(PK_iThongtinbaikiemtraID);
        }
        #endregion
        #region Private Methods
        private static void checkExist(Int64 PK_iThongtinbaikiemtraID)
        {
            ThongtinbaikiemtraEntity oThongtinbaikiemtra=ThongtinbaikiemtraDAL.GetOne(PK_iThongtinbaikiemtraID);
            if(oThongtinbaikiemtra==null)
                throw new Exception(EX_NOT_EXIST);
        }
        /// <summary>
        /// Kiểm tra logic Entity
        /// </summary>
        /// <param name="entity">ThongtinbaikiemtraEntity: entity</param>
        private static void checkLogic(ThongtinbaikiemtraEntity entity)
        {   
			if (entity.FK_iHocvienID < 0)
				throw new Exception(EX_FK_IHOCVIENID_INVALID);
			if (entity.FK_iKiemtraID < 0)
				throw new Exception(EX_FK_IKIEMTRAID_INVALID);
			if (entity.fDiem < 0)
				throw new Exception(EX_IDIEM_INVALID);
			if (entity.iTrangthai < 0)
				throw new Exception(EX_ITRANGTHAI_INVALID);
			if (DateTime.Parse("1753-01-01")>entity.dThoigiannopbai)
				throw new Exception(EX_DTHOIGIANNOPBAI_INVALID);
			if (entity.FK_iGiangvienchambaiID < 0)
				throw new Exception(EX_FK_IGIANGVIENCHAMBAIID_INVALID);
        }
        /// <summary>
        /// Kiểm tra trùng lặp bản ghi
        /// </summary>
        /// <param name="entity">ThongtinbaikiemtraEntity: ThongtinbaikiemtraEntity</param>
        private static void checkDuplicate(ThongtinbaikiemtraEntity entity,bool checkPK)
        {
            /* 
            Example
            List<ThongtinbaikiemtraEntity> list = ThongtinbaikiemtraDAL.GetAll();
            if (list.Exists(
                delegate(ThongtinbaikiemtraEntity oldEntity)
                {
                    bool result =oldEntity.FIELD.Equals(entity.FIELD, StringComparison.OrdinalIgnoreCase);
                    if(checkPK)
                        result=result && oldEntity.PK_iThongtinbaikiemtraID != entity.PK_iThongtinbaikiemtraID;
                    return result;
                }
            ))
            {
                list.Clear();
                throw new Exception(EX_FIELD_EXISTED);
            }
            */
        }
        /// <summary>
        /// Kiểm tra tồn tại khóa ngoại
        /// </summary>
        /// <param name="entity">ThongtinbaikiemtraEntity:entity</param>
        private static void checkFK(ThongtinbaikiemtraEntity entity)
        {            
			NguoidungEntity oNguoidung = NguoidungDAL.GetOne(entity.FK_iHocvienID);
			if (oNguoidung==null)
			{
				throw new Exception("Không tìm thấy :FK_iHocvienID");
			}
			KiemtraEntity oKiemtra = KiemtraDAL.GetOne(entity.FK_iKiemtraID);
			if (oKiemtra==null)
			{
				throw new Exception("Không tìm thấy :FK_iKiemtraID");
			}
			oNguoidung = NguoidungDAL.GetOne(entity.FK_iGiangvienchambaiID);
			if (oNguoidung==null)
			{
				throw new Exception("Không tìm thấy :FK_iGiangvienchambaiID");
			}
        }
        #endregion
    }
}
